<?php
/**
 * 员工转岗
 * @author ray
 */
apf_require_controller("HrAbstract");

class Hr_Staff_TransferController extends HrAbstractController {
    private $bll_user;
    private $bll_property;
    private $bll_job;
    private $bll_dep;
    private $bll_relation;

    private $user_id;

    public function handle_request() {
        $params = $this->request->get_parameters();

        $this->bll_user = new Bll_Hr_User();
        $this->bll_relation = new Bll_Hr_UserRelation();
        $this->bll_job = new Bll_Hr_Job();

        $this->user_id = intval($params['user_id']);

        if(!$this->is_post_request()){//展示表单
            $this->_show_form($params);
        }
        else{//提交表单
            $this->_deal_post($params);
        }
        //更新基本信息
        return "Hr_Staff_Transfer";
    }

    /**
     * 展示表单
     * @param array $params 参数
     */
    private function _show_form($params){
        //查询用户任职信息
        $work_info = $this->bll_user->get_user_work($this->user_id);
        if(!$work_info){
            $this->show_tips('NO_WORK_ERR');
        }
        $this->request->set_attribute('work_info',$work_info);

        //查询用户信息
        $user_info = $this->_get_user_info($this->user_id);
        $this->request->set_attribute('user_info',$user_info);

        //获得职位信息
        $job_info = $this->bll_job -> find_by_job_id($work_info['job_id']);
        $this->request->set_attribute('job_info',$job_info);

        //查询手下个数
        $sub_info = array();
        $sub_info['count'] = $this->bll_relation -> get_user_sub_count($this->user_id, $job_info['job_id']);
        $sub_info['url'] = Util_BaseUrls::build_search_url('parent_id', $this->user_id);
        $this->request->set_attribute('sub_info',$sub_info);

        //获取领导信息
        $parent_info = $this->_get_parent($this->user_id, $job_info['job_id']);
        $parent_info_is = isset($parent_info['is']) ? array_pop($parent_info['is']) : array();
        $parent_info_no = isset($parent_info['no']) ? array_pop($parent_info['no']) : array();
        $this->request->set_attribute('parent_info_is',$parent_info_is);
        $this->request->set_attribute('parent_info_no',$parent_info_no);

        //获得部门信息
        $this->bll_dep = new Bll_Hr_Department();
        $dep_info = $this->bll_dep->get_department_by_id($job_info['department_id']);
        $this->request->set_attribute('dep_info',$dep_info);
    }

    /**
     * 获得用户基础信息
     * @param unknown_type $user_id
     */
    private function _get_user_info($user_id){
        $user_info = $this->bll_user->get_user_by_id($user_id);
        $this->bll_property = new Bll_Hr_Property();
        $city_list = $this->bll_property->get_city_list();
        $p_list = $this->bll_property->get_p_degree();
        $m_list = $this->bll_property->get_m_degree();
        $this->request->set_attribute('p_list',$p_list);
        $this->request->set_attribute('m_list',$m_list);

        $user_info['work_city_name'] = $city_list[$user_info['work_city_id']]['city_name'];
        $user_info['p_name'] = $p_list[$user_info['p_level']]['degree'];
        $user_info['m_name'] = $m_list[$user_info['m_level']]['degree'];
        return $user_info;
    }
    /**
     * 获取领导信息
     * @param unknown_type $user_id
     * @param unknown_type $job_id
     */
    private function _get_parent($user_id,$job_id){
        //获得当前直属领导和领导的职位
        $parent_info = array();
        $parent_temp = array();
        $job_temp = array();
        $parent_temp = $this->bll_relation -> get_user_parent($user_id, $job_id, 3);
        $parent_ids = array();
        foreach($parent_temp as $row){
            $parent_ids[] = $row['parent_id'];
            $job_temp[] = $row['parent_job_id'];
        }
        $user_temp = $this->bll_user->get_user_by_ids($parent_ids);//批量获取领导姓名
        $temp_job = $this->bll_job -> find_by_job_ids($job_temp);//批量获取领导职位信息（名称）
        foreach($parent_temp as $row){//拼装数据
            if($row['is_direct'] == 1){//直接主管
                $parent_info['is'][$row['relation_id']] = $row;
                $parent_info['is'][$row['relation_id']]['parent_name'] = $user_temp[$row['parent_id']]['user_name'];
                $parent_info['is'][$row['relation_id']]['position_name'] = $temp_job[$row['parent_job_id']]['position_name'];
            }else{//虚线主管
                $parent_info['no'][$row['relation_id']] = $row;
                $parent_info['no'][$row['relation_id']]['parent_name'] = $user_temp[$row['parent_id']]['user_name'];
                $parent_info['no'][$row['relation_id']]['position_name'] = $temp_job[$row['parent_job_id']]['position_name'];
            }
        }
        return $parent_info;
    }

    /**
     * 处理POST来的数据
     * @param array $params 参数
     */
    private function _deal_post($params){
        $this->_check_params($params);//检测必要参数
        $new_job_id = intval($params['new_job_id']);
        $old_job_id = intval($params['old_job_id']);
        if($new_job_id == $old_job_id){
            $this->show_tips('NO_CHANGE_WORK');
        }
        $new_parents = $this->_check_parents($params);//检测并返回新主管数据
        //更新用户P/M信息等
        $re = $this->_update_user_info($params);
        if($re){
            //删除上级关系
            $re = $this->bll_relation->delete_parent_relation($this->user_id, $old_job_id);
            if(!$re){
                $this->show_tips('DELETE_RELATION_ERR');
            }
            //删除旧WORK信息
            $re = $this->bll_user -> delete_user_work($this->user_id, intval($params['work_id']));
            if($re){
                //查询是否还有主职存在
                $re = $this->bll_user->get_user_work($this->user_id, 1, 1);
                if($re){
                    $this->show_tips('DIRECT_WORK_ERR');
                }
                //添加新的work信息
                $re = $this->bll_user -> add_user_work($this->user_id, $new_job_id, intval(str_replace('-', '', $params['work_time'])));
                if($re){
                    //更新连带下级关系中的职位
                    $re = $this->bll_relation->update_sub_relation($this->user_id,$old_job_id,$new_job_id);
                    if(!$re){
                        $this->show_tips('UPDATE_RELATION_ERR');
                    }
                    //循环添加上级
                    foreach($new_parents as $row){
                        $re = $this->bll_relation->add_user_report_relation($this->user_id, $new_job_id, intval($row['parent_id']), intval($row['parent_job']),$row['parent_direct']);
                        if(!$re){
                            $this->show_tips('ADD_RELATION_ERR');
                        }
                        if($row['parent_direct'] == 1){
                            $direct_parent_id = intval($row['parent_id']);
                            $direct_parent_job = intval($row['parent_job']);
                        }
                    }
                    if($direct_parent_id && $direct_parent_job){
                        //写日志
                        $bll_log = new Bll_Hr_UnusualLog();
                        $bll_log->save_unusual_log($this->user_id, $new_job_id, $direct_parent_id, $direct_parent_job, 1);
                    }else{
                        //缺少写日志参数
                    }
                    $this->show_tips('OPT_SUCCESS', '', 1);//操作成功
                }else{
                    $this->show_tips('ADD_WORK_ERR');
                }
            }else{
                $this->show_tips('DELET_WORK_ERR');
            }

        }else{
            $this->show_tips('UPDATE_USER_ERR');
        }
    }
    /**
     * 检查主管数组，正确则返回
     * @param array $params
     * @return multitype:multitype:NULL
     */
    private function _check_parents($params){
        $parents = array();
        $repeat_flag = false;//重复上级
        $self_flag   = false;//自己当上级
        $direct_flag = 0;//直接上级数量
        $repeat_array = array();//检查重复
        foreach($params['parent_input'] as $index => $val){
            if(trim($val)){
                //检查重复
                $repeat_str = $params['parent_id'][$index].'|'.$params['parent_job'][$index];
                if(in_array($repeat_str, $repeat_array)){//检查重复添加主管
                    $repeat_flag = true;
                    break;
                }else{
                    $repeat_array[] = $repeat_str;
                }

                $parents[] = array(
                    'parent_id' => $params['parent_id'][$index],
                    'parent_job' => $params['parent_job'][$index],
                    'parent_direct' => $params['parent_direct'][$index],
                );



                if($params['parent_direct'][$index] == 1){//检查只有一个直线主管
                    $direct_flag++;
                }
                if($params['parent_id'][$index] == $this->user_id){
                    $self_flag = true;
                    break;
                }
            }else{
                continue;
            }
        }
        if(($direct_flag != 1) || empty($parents) || $repeat_flag || $self_flag){
            $this->show_tips('ERROR_REPEAT_PARENT');
        }else{
            return $parents;
        }
    }
    /**
     * 检查参数
     */
    private function _check_params($params){
        if(
                empty($params['work_id']) ||
                empty($params['work_time']) ||
                empty($params['old_job_id']) ||
                empty($params['new_job_id']) ||
                empty($params['old_city']) ||
                empty($params['new_city'])
        ){
            $this->show_tips('ERROR_PARAM');
        }
    }
    /**
     * 更新用户信息
     * @param unknown_type $params
     * @return boolean
     */
    private function _update_user_info($params){
        $re = true;
        $user_info = array();
        if($params['old_p'] != $params['new_p'])//如果P/M没更新则不更新
        {
            //更新员工信息
            $user_info['p_level'] = intval($params['new_p']);

        }
        if($params['old_m'] != $params['new_p']){
            $user_info['m_level'] = intval($params['new_m']);
        }
        if(($params['old_city'] != $params['new_city'])){
            $user_info['work_city_id'] = intval($params['new_city']);
        }
        if($user_info){
            $re = $this->bll_user -> update_user($this->user_id, $user_info);
        }
        return $re;
    }
}